#include "menu.h"
#include "ui_menu.h"

Menu::Menu(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Menu)
{
    ui->setupUi(this);
    ui->widget_content->hide();
    connectSignalsAndSlots();
}

Menu::~Menu()
{
    delete ui;
}

void Menu::connectSignalsAndSlots()
{
    connect(ui->pb_returnZero, &QPushButton::clicked, this, &Menu::pushReturnZero);
    connect(ui->pb_manual, &QPushButton::clicked, this, &Menu::pushManual);
    connect(ui->pb_automatic, &QPushButton::clicked, this, &Menu::pushAutomatic);

    connect(ui->pb_content_userchange, &QPushButton::clicked, this, &Menu::pushUserSwitch);
    connect(ui->pb_content_sysset, &QPushButton::clicked, this, &Menu::pushSystemSetting);
    connect(ui->pb_content_externalmemory, &QPushButton::clicked, this, &Menu::pushExternalMemory);
    connect(ui->pb_content_caltimer, &QPushButton::clicked, this, &Menu::pushCalTimer);
    connect(ui->manualReturnCaltimer, &QPushButton::clicked, this, &Menu::pushCalTimer);
    connect(ui->pb_content_teachmode,&QPushButton::clicked,this, &Menu::pushTeachMode);
    connect(ui->pb_content_programedit,&QPushButton::clicked,this, &Menu::pushProgramEditor);
    connect(ui->manualReturnTeachMode, &QPushButton::clicked, this, &Menu::pushTeachMode);
    connect(ui->manualReturnProgramEdit, &QPushButton::clicked, this, &Menu::pushProgramEditor);
    connect(ui->pb_content_speedset, &QPushButton::clicked, this, &Menu::pushSpeedAddDec);

    connect(ui->TCPControl_button,&QPushButton::clicked,this, &Menu::flipTcpCommunication);
    connect(ui->TCPAbort_button,&QPushButton::clicked,this, &Menu::abortTcpCommunication);
    connect(ui->pb_rs422Communication,&QPushButton::clicked,this, &Menu::pushRS422Communication);

    connect(ui->pb_content, &QPushButton::clicked, this, &Menu::changeContentDisplayState);

    connect(ui->pb_content_SystemSetting, &QPushButton::clicked, this, [&](){
        ui->stackedWidget->setCurrentIndex(0);
    });
    connect(ui->pb_content_TeachPage, &QPushButton::clicked, this, [&](){
        ui->stackedWidget->setCurrentIndex(1);
    });
    connect(ui->pb_content_IOHistory, &QPushButton::clicked, this, [&](){
        ui->stackedWidget->setCurrentIndex(2);
    });
}

void Menu::pushCalTimer()
{
    ui->widget_content->hide();
    emit go2CalTimer();
}

void Menu::pushExternalMemory()
{
    ui->widget_content->hide();
    emit go2ExternalMemory();
}

void Menu::pushSystemSetting()
{
    ui->widget_content->hide();
    emit go2SystemSetting();
}

void Menu::pushReturnZero()
{
    ui->widget_content->hide();
    emit go2ReturnZero();
}

void Menu::pushManual()
{
    ui->widget_content->hide();
    emit go2Manual();
}

void Menu::pushAutomatic()
{
    ui->widget_content->hide();
    emit go2Automatic();
}

void Menu::pushUserSwitch()
{
    ui->widget_content->hide();
    emit go2UserSwitch();
}

void Menu::pushTeachMode()
{
    ui->widget_content->hide();
    emit go2TeachMode();
}

void Menu::pushProgramEditor()
{
    ui->widget_content->hide();
    emit go2ProgramEditor("自动运转程序");
}

void Menu::pushSpeedAddDec()
{
    ui->widget_content->hide();
    emit go2SpeedAddDec();
}

void Menu::flipTcpCommunication()
{
    if(ui->TCPControl_button->text().toUtf8() == "已开始TCP，点击暂停")
    {
        emit IfTcpCommunicationGoOn(false);
        ui->TCPControl_button->setText("已暂停TCP，点击继续");
    }
    else if(ui->TCPControl_button->text().toUtf8() == "已暂停TCP，点击继续")
    {
        emit IfTcpCommunicationGoOn(true);
        ui->TCPControl_button->setText("已开始TCP，点击暂停");
    }
}

void Menu::abortTcpCommunication()
{
    emit AbortTcpConnect();
}

void Menu::pushRS422Communication()
{
    ui->widget_content->hide();
    emit go2RS422Communication();
}

void Menu::changeContentDisplayState()
{
    if(ui->widget_content->isHidden())
    {
        ui->widget_content->show();
    }
    else
    {
        ui->widget_content->hide();
    }
}
